<?php
	require_once "../includes/class/member_management.class.php";
require_once '../includes/class.phpmailer.php';

	$userId = $_GET['editmem'];	
	$usrObj = new member_management;
	if(isset($_POST['changeStatus']))
	{
	  $sqlSelUsr="SELECT email FROM tbl_users WHERE user_id='$userId'";
  $rstSelUsr=mysql_query($sqlSelUsr);
  $noSelUsr=mysql_num_rows($rstSelUsr);
  $rowEmailuser=mysql_fetch_array($rstSelUsr);
  $email=$rowEmailuser['email'];
	   $statusToChange=$_POST['accountStatus'];
	   $sqlUpdUsr = "UPDATE tbl_users SET status=".$statusToChange." WHERE user_id='$userId'";
	   $rstUpdUsr = mysql_query($sqlUpdUsr) or die(mysql_error());
	   if($rstUpdUsr) { $msg = "User account status changed successfully."; 
	   if($statusToChange=="3")
	   {
	    $sqlEmail="select * from tbl_emails where email_type='OnHold'";
		 $resEmail=mysql_query($sqlEmail);
	 $rowEmail=mysql_fetch_array($resEmail);
	  $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
  <tr>
    <td width='18%' height='41'><table width='100%' border='0'>
      <tr>
        <td width='19%' height='68'><img src='http://www.swapen.com/images/logo.gif'></td>
        <td width='81%'> </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$rowEmail['email_body']."</td>
  </tr>
  
 
</table>
";
		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = $rowEmail['email_subject'];
		$mail->AddAddress($email, $firstName);
		$mail->Send();
		}
		if($statusToChange=="4")
	   {
	    $sqlEmail="select * from tbl_emails where email_type='InActive'";
		 $resEmail=mysql_query($sqlEmail);
	 $rowEmail=mysql_fetch_array($resEmail);
	  $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
  <tr>
    <td width='18%' height='41'><table width='100%' border='0'>
      <tr>
        <td width='19%' height='68'><img src='http://www.swapen.com/images/logo.gif'></td>
        <td width='81%'> </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$rowEmail['email_body']."</td>
  </tr>
  
 
</table>
";
		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = $rowEmail['email_subject'];
		$mail->AddAddress($email, $firstName);
		$mail->Send();
		}
	
	   }
		else { $msg=""; }
	}	
	$rand = rand(100000, 10000000);
	if(isset($_POST['editProfile'])) {
		foreach($_POST as $key=>$value) {
			$$key=$value;			
		}
		
		$profileImage = $_FILES['profile_image']['name'];
		if($profileImage!="") {
			$pathInfo = pathinfo($profileImage);
			$ftype = strtolower($pathInfo['extension']);
			if($ftype=="jpeg" || $ftype=="jpg" || $ftype=="gif") {
			
				$pathSource = "../users/source/".$rand.$userId.".".$ftype;
				$pathSample = "../users/sample/".$rand.$userId.".".$ftype;
				$pathThumb = "../users/thumb/".$rand.$userId.".".$ftype;
				
				if(move_uploaded_file($_FILES['profile_image']['tmp_name'],$pathSource)) {
					//get and delete old images
					$userImages = $usrObj->getUserImage($userId);
					print_r($userImages);
					
					if($userImages[0]!="") {
						unlink($userImages[0]);
					} 
					if($userImages[1]!="") {
						 unlink($userImages[1]);
					}
					
					
					imageResize($pathSource, $pathThumb, $pathSample, $ftype);
					
					$sqlUpd = "UPDATE tbl_users SET profile_sample='$pathSample', profile_thumb='$pathThumb' 
							   where user_id='$userId'";
					$rstUpd = mysql_query($sqlUpd);
					if($rstUpd) {
						unlink($pathSource);					
					}		   
				}
			}
		}
		
		//change state id to state name
		if(is_numeric($state)) {
			$state = getStateName($state);
		} 
		//change city id to city name
		if(is_numeric($city)) {
			$city = getCityName($city);
		}
		
		$phone=$phoneNumber1."-".$phoneNumber2."-".$phoneNumber3;
		if(!empty($extension)) {
			$phone.="-".$extension;
		}
		
		$sqlUpdUsr = "UPDATE tbl_users SET first_name='".$firstName."',
					last_name='".$lastName."',
					password='".$currentPass."',
					address_line_1='".$address1."',
					address_line_2='".$address2."',					
					city='".$city."',
					state='".$state."',
					country='".$country."',
					zip='".$zip."',
					phone_number_1='".$phone."',
					age='".$age."',
					about_me='".$aboutMe."',
					secret_question='".$secretQuestion."',
					secret_answer='".$secretAnswer."',
					bus_id='".$btype."',
					other_business_type='".addslashes($other_business_type)."',
					email='".$email."' WHERE user_id='$userId'";
		$rstUpdUsr = mysql_query($sqlUpdUsr) or die(mysql_error());
		if($rstUpdUsr) { $msg = "User profile updated successfully."; }
		else { $msg=""; }					
		
		/* if($usrObj->updateUserInfo($userId,$firstName,$lastName,$address1,$address2,$city,$state,$country,$zip,$phone,$email,$age,$aboutMe)) {
			$msg = "Your profile updated successfully.";
		}
		else {
			$msg="";
		}		 */
		
	}
	
	$userInfo = $usrObj->getMemberById($userId);
	$phoneArr = explode("-",$userInfo['phone_number_1']);
	//print_r($userInfo);
	
?>

<table width="100%" height="611"  border="1" cellspacing="0" class='container'>
  <tr>
  	<td height="26" class="tdHeader">Members Profile </td>
  </tr>
  <tr>
    <td>
		
		<table width="100%" border="0" class="content">
		  <tr  class="content">
			<td height="32" colspan="3"><div id="messageArea"><?php if(isset($msg))
												echo "<font color='#0000FF'><b>".$msg."</b></font>";
											
			?></div></td>
			</tr>
            <form enctype="multipart/form-data" name="frmEditProfile" method="post" action="<?php echo $_SERVER['php_self']?>">
		  <tr  class="content">
		    <td height="40">Uername : <strong><?php echo $userInfo['username']; ?></strong></td>
		    <td>A/C Status : 
		    <select name="accountStatus" id="accountStatus">
            <?php			   
				   $sqlSelUserStatus="SELECT * FROM tbl_user_status WHERE status_id!=0 AND status_id!=2 AND status_id!=5";
				   $rstSelUserStatus=mysql_query($sqlSelUserStatus);
				   while($rowSelUserStatus=mysql_fetch_array($rstSelUserStatus))
				   {
					   $statusId=$rowSelUserStatus['status_id'];
					   $status=$rowSelUserStatus['status'];
					   if($userInfo['status']==$statusId) {
					   		$sel = "selected";
					   } else { $sel = ""; }
					   echo "<option value='$statusId' $sel>$status</option>";
					   
				   }
				   ?>
            </select>            </td>
		    <td><input type="submit" name="changeStatus" id="changeStatus" value="Change status" /></td>
	      </tr>
          </form>
          <form enctype="multipart/form-data" name="frmEditProfile" method="post" action="<?php echo $_SERVER['php_self']?>" onSubmit="return funEditProfile();">
		  <tr  class="content">
			<td width="32%"><div id="lblFirstName">First name</div>
			<input type="text" name="firstName" id="firstName" value="<?php echo $userInfo['first_name']; ?>"></td>
			<td width="44%"><div id="lblLastName">Last name</div>
		    <input type="text" name="lastName" id="lastName" value="<?php echo $userInfo['last_name']; ?>"></td>
			<td width="24%">&nbsp;</td>
		  </tr>
		  <tr  class="content">
		    <td><div id="lblCurPass">Password</div>
				<input type="text" name="currentPass" id="currentPass" value="<?php echo $userInfo['password']; ?>">			</td>
		    <td>&nbsp;</td>
		    <td>&nbsp;</td>
	      </tr>
		  <tr>
			<td  height="10"></td>
			<td></td>
			<td></td>
		  </tr>
		  <tr>
			<td colspan="2"><div id="lblAddress1">Address line 1</div><input type="text" name="address1" id="address1" value="<?php echo $userInfo['address_line_1']; ?>"></td>
			<td>&nbsp;</td>
		  </tr>
		  
		  <tr>
			<td colspan="2"><input type="text" name="address2" id="address2" value="<?php echo $userInfo['address_line_2']; ?>"></td>
			<td>&nbsp;</td>
		  </tr>
		 <tr>
			<td  height="10"></td>
			<td></td>
			<td></td>
		  </tr>
		  <tr>
			<td><div id="lblCity">City</div><span id="selectedCity"><input type="text" name="city" id="city" value="<?php echo $userInfo['city']; ?>"></span></td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		  </tr>
		  <tr>
			<td></td>
			<td></td>
			<td></td>
		  </tr>
		  <tr>
			<td><div id="lblCountry">Country</div>				
			<!-- <input type="text" name="country" id="country" value="<?php echo $userInfo['country']; ?>"> -->
				<select name="country" id="country" onChange="javascript:funSelectState(this.value,'selectedState')" style="width:145px;">
				   <option value="">Select country...</option> 
				   <?php			   
				   $sqlSelCountry="SELECT * FROM tbl_country";
				   $rstSelCountry=mysql_query($sqlSelCountry);
				   while($rowSelCountry=mysql_fetch_array($rstSelCountry))
				   {
					   $cty_id=$rowSelCountry['country_id'];
					   $cty_name=$rowSelCountry['country_name'];
					   if($userInfo['country']==$cty_id) {
					   		$sel = "selected";
					   } else { $sel = ""; }
					   echo "<option value='$cty_id' $sel>$cty_name</option>";
					   
				   }
				   ?>
		   		</select>			</td>
			<td><div id="lblState">State/ Province</div><span id="selectedState"><input type="text" name="state" id="state" value="<?php echo $userInfo['state']; ?>"></span></td>
			<td><div id="lblZip">Postal / zip code</div><input type="text" name="zip" id="zip" value="<?php echo $userInfo['zip']; ?>"></td>
		  </tr>
		  <tr>
			<td  height="10"></td>
			<td></td>
			<td></td>
		  </tr>
		  <? if($userInfo['user_type']==3)
		     {
		  ?>
		  <tr>
			<td colspan="2"><div id="lblPhoneNumber">Business type</div>
				<select name="btype" id="btype" style="width:145px;" onchange="business_other(this.value);">
				   <option value="">Select business type...</option> 
				   <?php			   
				   $sqlbtype="SELECT * FROM tbl_businesstype ORDER BY business_type";
				   $rstSbtype=mysql_query($sqlbtype);
				   while($rowSelbtype=mysql_fetch_array($rstSbtype))
				   {
					   $bus_id=$rowSelbtype['bus_id'];
					   $business_type=$rowSelbtype['business_type'];
					   if($userInfo['bus_id']==$bus_id) {
					   		$sel = "selected";
					   } else { $sel = ""; }
					   echo "<option value='$bus_id' $sel>$business_type</option>";
					   
				   }
				   ?>
		   		</select>
			</td>
			<td>&nbsp;</td>
		  </tr>
		  
		  <?php if($userInfo['bus_id']=='59') { ?>
          <tr>
			<td colspan="2"><div id="lblPhoneNumber">Specification</div>
				<input type="text" name="other_business_type" id="other_business_type" value="<?php echo stripslashes($userInfo['other_business_type']); ?>">
			</td>
			<td>&nbsp;</td>
		  </tr>
		  <?php } ?>
		  <?
		     }
		  ?>
		 <tr>
			<td  height="10"></td>
			<td></td>
			<td></td>
		  </tr>
		  <tr>
			<td colspan="2"><div id="lblPhoneNumber">Phone number</div><input type="text" value="<?php echo $phoneArr[0]; ?>" name="phoneNumber1" id="phoneNumber1" maxlength="4" size="3">&nbsp;/&nbsp;<input type="text" name="phoneNumber2" id="phoneNumber2" value="<?php echo $phoneArr[1]; ?>" maxlength="4" size="3">&nbsp;/&nbsp;<input type="text" name="phoneNumber3" value="<?php echo $phoneArr[2]; ?>" id="phoneNumber3" maxlength="6" size="4">&nbsp;&nbsp;&nbsp;Ext : <input type="text" name="extension" value="<?php echo $phoneArr[3]; ?>" id="extension" maxlength="20" size="5"></td>
			<td>&nbsp;</td>
		  </tr>
		 <tr>
			<td  height="10"></td>
			<td></td>
			<td></td>
		  </tr>	 
		  <tr>
			<td><div id="lblEmail">Email ID</div>
				<input type="text" value="<?php echo $userInfo['email']; ?>" name="email" id="email" /></td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		  </tr>
		  <tr>
            <td  height="10"></td>
		    <td></td>
		    <td></td>
		    </tr>
             <?php if($userInfo['user_type']=='1')
                    { ?>
		  <tr>
            <td colspan="2"><div id="lblSecretQuestion">Secret question</div>
                <!-- <input type="text" name="country" id="country" value="<?php echo $userInfo['country']; ?>"> -->
                <select name="secretQuestion" id="secretQuestion" style="width:300px">
            <?php
			$sqlSelQuestion = "select * from tbl_questions";
			$rstSelQuestion = mysql_query($sqlSelQuestion) or die(mysql_error());
			while($rowSelQuestion = mysql_fetch_array($rstSelQuestion))
			{
			 if($userInfo['secret_question']==$rowSelQuestion['question_id']) {
					   		$sel = "selected";
					   } else { $sel = ""; }
					   echo "<option value=".$rowSelQuestion['question_id']." ".$sel.">".$rowSelQuestion['question']."</option>";
 } ?>
          </select>            </td>
		    <td></td>
		  </tr>
		  <tr>
            <td  height="10"></td>
		    <td></td>
		    <td></td>
		    </tr>
		  <tr>
		    <td height="60" ><div id="lblSecretAnswer">Answer</div>
		          <input type="text" name="secretAnswer" id="secretAnswer" value="<?php echo $userInfo['secret_answer']; ?>" />
	         </td>
		    <td>&nbsp;</td>
		    <td></td>
		    </tr>
            <?php } ?>
		  <tr>
			<td height="60" >
				<div id="lblProfileImage">Profile image &nbsp;&nbsp;<?php if($userInfo['profile_sample']!="") { ?><a href="../<?php echo $userInfo['profile_sample']; ?>">View</a><?php } ?></div>
				<input type="file" name="profile_image" id="profile_images">			</td>
			<td><div id="lblAge">Age</div><input type="text" name="age" id="age" value="<?php echo $userInfo['age']; ?>"></td>
			<td></td>
		  </tr>
		  <tr>
		    <td ><div id="lblAboutMe">About me</div>
				<textarea name="aboutMe" id="aboutMe"><?php echo $userInfo['about_me']; ?></textarea>			</td>
		    <td>&nbsp;</td>
		    <td></td>
		    </tr>		 
		  <tr>
			<td  height="10"></td>
			<td></td>
			<td></td>
		  </tr>		
		  <tr>
			<td><input type="submit" name="editProfile" id="editProfile" value="Update" />&nbsp;&nbsp;<a href="index.php?viewpro">Preview</a></td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		  </tr>		
          </form>
		</table>
	  
	</td>
  </tr>
</table>  
<?php if($userInfo['bus_id']!='59') { ?>
<script language="javascript" type="text/javascript">
function business_other(bus)
{
	if(bus==59)
	{
	  document.getElementById('other_business').style.display='';
	}
	else
	{
	  document.getElementById('other_business').style.display='none';
	}
}
</script>	
<?php  } ?>